CS 211 Lesson 13

Function Types and Scope

Quote:

Truth does not become more true by virtue of the fact that the entire world agrees with it, nor less so even if the whole world disagrees with it. Maimonides

Lesson Objectives:

Lesson:

I. MATLAB Concepts

A. Function Overview

B. Types of functions

C. MATLAB's search order to find functions

  1. first, MATLAB looks for a nested function inside the current function
  2. then, MATLAB looks for a subfunction
  3. then, MATLAB looks for a private function
  4. then, MATLAB looks for a file with the function name in the current directory (folder)
  5. then, MATLAB looks for a file with the function name in any directory (folder) in its search path
  6. then, if all this failed, MATLAB issues an error message: "function not found"

D. function functions

for n = 1:12
   magic_str = ['M',int2str(n),' = magic(n)'];
   eval(magic_str)
end

x = 0:0.1:10;
for n = 1:5
   y = eval([int2str(n),'.*sin(',int2str(n),'.*x)']);
   plot(x,y)
   hold on
end

II. Good Programming Practices

A. Limit Scope

B. Use the appropriate type of function

III. Algorithms

 

Lab Work: Lab 13

References:  Chapman Textbook: section 5.7-5.8